package etxt2db.mallet;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import edu.cmu.minorthird.classify.Feature;
import edu.cmu.minorthird.text.Span;
import edu.cmu.minorthird.text.TextLabels;
import edu.cmu.minorthird.text.learn.SpanFeatureExtractor;
import edu.umass.cs.mallet.base.pipe.Pipe;
import edu.umass.cs.mallet.base.types.Instance;
import edu.umass.cs.mallet.base.types.TokenSequence;

public class MalletFeatureExtractor extends Pipe implements Serializable
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2713912120829644369L;
	TextLabels labels;
	List<String> attributes;
	SpanFeatureExtractor tokenFE;
	
	public MalletFeatureExtractor (TextLabels labels, List<String> attributes, SpanFeatureExtractor tokenFE)
	{
		this.labels = labels;
		this.attributes = attributes;
		this.tokenFE = tokenFE;
	}
	

	public Instance pipe (Instance carrier)
	{
		//Iterator<Span> iter = labels.getTextBase().documentSpanIterator();
		TokenSequence ts = (TokenSequence) carrier.getData();
		//while(iter.hasNext()){
			Span current = labels.getTextBase().documentSpan((String)carrier.getSource());
			int i = 0;
			int seq=0;
			while(i<current.size()){
				Span tok = current.subSpan(i, 1);
				if(!tok.asString().matches("\\s*")){
					edu.cmu.minorthird.classify.Instance ins = tokenFE.extractInstance(labels, tok);
					Iterator<Feature> iterator = ins.featureIterator();
					while(iterator.hasNext()){
						Feature f = iterator.next();
						String featureName = "";
						int j = 0;
						while(j<f.getName().length){
							if(j==0){
								featureName=featureName+f.getName()[j];
							}else{
								featureName =featureName+ "_" + f.getName()[j];
							
							}
							j++;
						}
						ts.getToken(seq).setFeatureValue(featureName, 1.0);
					}
					seq++;
				}
				i++;
			}
		//}
		return carrier;
	}
}

